package com.course.business.controller.admin;

import com.course.server.domain.Category;
import com.course.server.dto.CategoryDto;
import com.course.server.dto.PageDto;
import com.course.server.dto.ResponseDto;
import com.course.server.service.CategoryService;
import com.course.server.util.ValidatorUtil;
import org.apache.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zht
 * @create 2021-09-05 15:58
 */
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

private static final Logger LOG = LoggerFactory.getLogger(CategoryController.class);
    public static final String BUSINESS_NAME =  "分类";

    @Resource
    private CategoryService categoryService;

    /**
     * 列表查询
     */
    @RequestMapping("/list")
    public ResponseDto list(PageDto pageDto){
        ResponseDto responseDto = new ResponseDto();
        categoryService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }
    @RequestMapping("/all")
    public ResponseDto all(){
        ResponseDto responseDto = new ResponseDto();
        List<CategoryDto> categoryFtoList = categoryService.all();
        responseDto.setContent(categoryFtoList);
        return responseDto;
    }
    /**
     * 保存，id有值时更新，无值时新增
     */
    @RequestMapping("/save")
    public ResponseDto save(CategoryDto categoryDto){

        // 保存校验
            ValidatorUtil.require(categoryDto.getParent(), "父id");
            ValidatorUtil.require(categoryDto.getName(), "名称");
            ValidatorUtil.length(categoryDto.getName(), "名称", 1, 50);


        ResponseDto responseDto = new ResponseDto();
        categoryService.save(categoryDto);
        responseDto.setContent(categoryDto);
        return responseDto;
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResponseDto delete(String id){
        System.out.println(id);
        ResponseDto responseDto = new ResponseDto();
        categoryService.delete(id);
        return responseDto;
    }

}
